package algorithms;

import java.util.HashMap;
import java.util.List;

/**
 * @author lizen
 */
public class DateStringFormat {
    public static void test() {
        List<String> dateList = List.of("26th May 1960", "31st Dec 2020");
        dateList.stream().map(DateStringFormat::format)
                .forEach(System.out::println);
    }

    public static String format(String dateStr) {
        HashMap<String, String> monthMap = getMonthMap();
        String[] dayMonthYear = dateStr.split(" ");
        String day = getDayString(dayMonthYear[0]);
        String month = monthMap.get(dayMonthYear[1]);

        return dayMonthYear[2]+"-"+month+"-"+day;
    }

    public static String getDayString(String dayString) {
        int stopPos = 0;
        for(int count = 0; count < dayString.length(); count++) {
            if (isAlphabetChar(dayString.charAt(count))){
                break;
            }
            stopPos++;
        }

        return dayString.substring(0, stopPos);
    }

    public static boolean isAlphabetChar(char c){
        return 'a' <= c && c <= 'z';
    }

    public static HashMap<String, String> getMonthMap()
    {
        HashMap<String, String> monthMap = new HashMap<>();
        monthMap.put("Jan", "01");
        monthMap.put("Feb", "02");
        monthMap.put("Mar", "03");
        monthMap.put("Apr", "04");
        monthMap.put("May", "05");
        monthMap.put("Jun", "06");
        monthMap.put("Jul", "07");
        monthMap.put("Aug", "08");
        monthMap.put("Sep", "09");
        monthMap.put("Oct", "10");
        monthMap.put("Nov", "11");
        monthMap.put("Dec", "12");
        return monthMap;
    }
}
